/*
Copyright (c) 2012 the VATSIM Connector project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "libvatconn.h"
#include "connection.h"
#include "messages.h"
#include <iostream>

enum
{
  c_connectorPort = 2992,
  c_pluginPort
};

class Handler
{
public:
  void handleMessagePP(VconMsgPlanePosition const& pp)
  {
    std::cout << "RECV #PP"
      << " " << pp.callsign
      << " " << pp.airline
      << " " << pp.aircraft
      << " " << pp.lat
      << " " << pp.lon
      << " " << pp.alt
      << " " << pp.hdg
      << " " << pp.gears
      << " " << pp.flaps
      << " " << pp.lights << std::endl;
  }

  void handleMessageWX(VconMsgWeather const& wx)
  {
    switch (wx.subtype)
    {
    case vconWxMETAR:
      std::cout << "RECV #WX METAR " << wx.metar << std::endl;
      break;
    case vconWxCLOUDS:
      std::cout << "RECV #WX CLOUDS"
	<< " " << wx.clouds.layer
	<< " " << wx.clouds.base
	<< " " << wx.clouds.top
	<< " " << wx.clouds.coverage
	<< " " << wx.clouds.icing
	<< " " << wx.clouds.turbulence
	<< " " << wx.clouds.tsdev << std::endl;
      break;
    case vconWxWINDS:
      std::cout << "RECV #WX WINDS"
	<< " " << wx.winds.layer
	<< " " << wx.winds.base
	<< " " << wx.winds.top
	<< " " << wx.winds.direction
	<< " " << wx.winds.speed
	<< " " << wx.winds.gusting
	<< " " << wx.winds.turbulence << std::endl;
      break;
    case vconWxTEMPS:
      std::cout << "RECV #WX TEMPS"
	<< " " << wx.temps.layer
	<< " " << wx.temps.ceiling
	<< " " << wx.temps.temperature
	<< " " << wx.temps.pressure << std::endl;
      break;
    default:
      std::cerr << "Unrecognised #WX message subtype" << std::endl;
    }
  }

  void handleMessageMG(VconMsgText const& mg)
  {
    std::cout << "RECV #MG " << mg.message << std::endl;
  }

  void handleMessageSQ(VconMsgSquawk const& sq)
  {
    std::cout << "RECV #SQ " << sq.squawk.code << std::endl;
  }
};

void print(VconMsgOwnPlane const& op)
{
  std::cout << "SEND #OP"
    << " " << op.lat
    << " " << op.lon
    << " " << op.alt
    << " " << op.hdg
    << " " << op.gears
    << " " << op.flaps
    << " " << op.lights
    << " " << op.nearestAirport << std::endl;
}

void print(VconMsgRadioFreq const& op)
{
  std::cout << "SEND #RX " << op.radio << " " << op.frequency.kHz << std::endl;
}

void print(VconMsgChangeSquawk const& op)
{
  std::cout << "SEND #CQ " << op.squawk.code << " " << op.ident << " " << op.mode << std::endl;
}

void print(VconMsgText const& mg)
{
  std::cout << "SEND #MG " << mg.message << std::endl;
}

int test_conn(VconConnection* conn)
{
  int c;
  do
  {
    c = std::cin.get();
    if (c == 'r')
    {
      conn->pollReceivedMessages();
    }
    else if (c == 's')
    {
      VconMsgOwnPlane op;
      op.lat = 17.183056;
      op.lon = 42.817830;
      op.alt = 12750;
      op.hdg = 103;
      op.gears = false;
      op.flaps = false;
      op.lights = false;
      op.nearestAirport = "LOWW";
      print(op);
      conn->queueMessage(op);

      VconMsgRadioFreq rx;
      rx.radio = 1;
      rx.frequency.kHz = 119400;
      print(rx);
      conn->queueMessage(rx);

      VconMsgChangeSquawk cq;
      cq.squawk.code = 4062;
      cq.ident = false;
      cq.mode = true;
      print(cq);
      conn->queueMessage(cq);

      VconMsgText mg;
      mg.message = "And another example";
      print(mg);
      conn->queueMessage(mg);

      conn->sendQueuedMessages();
    }
  } while (! (c == 'x' || c == 'q' || c == '\033' || c == '\030' || c == '\03' || c == '\04'));

  return 0;
}

int test(VconConnection* conn)
{
  Handler h;
  conn->setMessageHandler(h);
  conn->setPorts(c_pluginPort, c_connectorPort);
  conn->setRemoteHost("127.0.0.1");
  conn->setUserPass("800000", "abcdefg");
  char const* err = conn->enable();
  if (err)
  {
    std::cerr << "Failed to enable connection: " << err << std::endl;
    return 1;
  }

  int result = test_conn(conn);

  conn->disable();
  return result;
}

int main()
{
  std::cin.sync();

  VconConnection* conn = VconConnection::create();
  int result = test(conn);
  delete conn;
  return result;
}